using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.AnalysisServices.AdomdClient;
using Microsoft.AnalysisServices.Viewers;
using Microsoft.ReportingServices.QueryDesigners;
using Microsoft.ReportingServices.ReportBuilder;
using RetailAnalytic.Core;

namespace RetailAnalytic.DM.Engine
{
    public partial class FrmDM : Form
    {
        public bool IsQuestClose = true;
        public bool isSave = true;
        public PresentationBase _presentationBase;
        public PresentationDM _pDM;
        public AdomdConnection conn = new AdomdConnection();
        public MiningModelViewerControl viewer = null;
        public MiningModel model = null;
        public MiningService service = null;
        public MiningStructure ms = null;
        bool StartUp = true;
        public FrmDM(PresentationBase presentationBase)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("");//RetailAnalytic.Core.Properties.Settings.Default.CurrentCulture.Name);
            InitializeComponent();
            _presentationBase = presentationBase;
        }
        private void SetGuiAppearance()
        {
            this.Icon = SR.ImagesLibrary.GetIconByName("DMStructureColumn");
            this.ultraToolbarsManager1.Ribbon.Tabs["ribbDM"].Caption = _presentationBase.IGlobalModule.Name;
            this.ultraToolbarsManager1.Tools["btn_Close"].SharedProps.AppearancesSmall.Appearance.Image = SR.ImagesLibrary.GetImageByName("exit");
        }

        public void ExecuteDMPresentation()
        {
            SetGuiAppearance();
            _pDM = new PresentationDM(_presentationBase);
            #region Start Valid connection to AS
            try
            {
                conn.ConnectionString = _pDM.DataSource;
                conn.Open();
                string _s = conn.Database;
            }
            catch (Exception ex)
            {
                Dialog.Show(Dialog_type.Error, SR.StringLibrary.GetStrMSG(SR.StringLibrary.KeyEnum.Error_NO_CONNECT_OLAP,
                    Environment.StackTrace, ex));
                this.Close();
                return;
            }
            #endregion End Valid connection to AS
            this.Show();
            StartUp = true;
            ms = conn.MiningStructures[_pDM.MiningStructure];
            Infragistics.Win.UltraWinToolbars.ComboBoxTool cb;

            cb = (Infragistics.Win.UltraWinToolbars.ComboBoxTool)ultraToolbarsManager1.Toolbars[1].Tools[0];
            cb.ValueList.ValueListItems.Clear();
            for (int i = 0; i < ms.MiningModels.Count; i++)
            {
                if (_pDM.IncludedDMmodel.IndexOf(ms.MiningModels[i].Name) > -1)
                    cb.ValueList.ValueListItems.Add(ms.MiningModels[i].Name);
            }
            string[] arrDMmodels = _pDM.IncludedDMmodel.Split(new char[] { ';' });
            cb.SelectedIndex = 0;
            ShowModel(panel1, _pDM.MiningStructure, arrDMmodels[0]);
            StartUp = false;

        }
        private void ShowModel(Panel panel, string modelName, string ModelName)
        {
            
            try
            {
                // Clear any existing controls from the panel
                if (panel.HasChildren)
                    panel.Controls.Clear();

                // Connect to server

                // Determine the viewer type based on the model service and
                // instantiate the correct viewer
                ms = conn.MiningStructures[modelName];
                 Infragistics.Win.UltraWinToolbars.ComboBoxTool cb;
                
                 cb = (Infragistics.Win.UltraWinToolbars.ComboBoxTool)ultraToolbarsManager1.Toolbars[1].Tools[0];
                 //StartUp = true;
                 //cb.ValueList.ValueListItems.Clear();
                 //StartUp = false ;
                 //for (int i = 0; i < ms.MiningModels.Count; i++)
                 //{
                 //    cb.ValueList.ValueListItems.Add(ms.MiningModels[i].Name);
                 //}
                // cb.SelectedText = ModelName;
                 model = ms.MiningModels[cb.SelectedItem.ToString()];
                //model = conn.MiningModels[modelName];
                service = conn.MiningServices[model.Algorithm];
                
                if (service.ViewerType == "Microsoft_Cluster_Viewer")
                    viewer = new ClusterViewer();
                else if (service.ViewerType == "Microsoft_Tree_Viewer")
                    viewer = new TreeViewer();
                else if (service.ViewerType == "Microsoft_NaiveBayesian_Viewer")
                    viewer = new NaiveBayesViewer();
                else if (service.ViewerType == "Microsoft_SequenceCluster_Viewer")
                    viewer = new SequenceClusterViewer();
                else if (service.ViewerType == "Microsoft_TimeSeries_Viewer")
                    viewer = new TimeSeriesViewer();
                else if (service.ViewerType == "Microsoft_AssociationRules_Viewer")
                    viewer = new AssociationViewer();
                else if (service.ViewerType == "Microsoft_NeuralNetwork_Viewer")
                    viewer = new NeuralNetViewer();
                else throw new System.Exception("Custom Viewers not supported");
                string s = model.Algorithm;

                // Set up and load the viewer
                viewer.ConnectionString = _pDM.DataSource;
                //viewer.MiningModelName = modelName;
                viewer.MiningModelName = model.Name;
                viewer.Dock = DockStyle.Fill;
                panel.Controls.Add(viewer);
                //Beta2Fixup(viewer);
                viewer.LoadViewerData(null);
                //Beta2Fixup2(viewer);
            }
            catch (System.Exception ex)
            {
                Dialog.Show(Dialog_type.Error, SR.StringLibrary.GetStrMSG(SR.StringLibrary.KeyEnum.MESSAGE_EMPTY,
                    Environment.StackTrace, ex));
            }
            //conn.Close();
        }

        private void FrmDM_Load(object sender, EventArgs e)
        {

         }

        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {

                case "btn_Close":
                    {
                        this.Close();
                        break;
                    }
            }
        }

        private void ultraToolbarsManager1_ToolValueChanged(object sender, Infragistics.Win.UltraWinToolbars.ToolEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "cbMModel":    // ComboBoxTool
                    // Place code here
                    if (!StartUp)
                    {
                        Infragistics.Win.UltraWinToolbars.ComboBoxTool cb;
                        cb = (Infragistics.Win.UltraWinToolbars.ComboBoxTool)e.Tool;
                        ShowModel(panel1, _pDM.MiningStructure, cb.SelectedItem.ToString());
                    }
                    break;

            }
     
        }

        public void FrmDM_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (!_presentationBase.IGlobalModule.SaveClosePresentation(_presentationBase, false, false, true))
                {
                    e.Cancel = true;
                }
            }
            catch (Exception er)
            {
                Dialog.Show(Dialog_type.Error, SR.StringLibrary.GetStrMSG(RetailAnalytic.SR.StringLibrary.KeyEnum.MESSAGE_EMPTY,
                    Environment.StackTrace, er));
            }
        }
    }
}